//重写数组

// （1）获取原来的数组方法
let oldArrayProtoMethods = Array.prototype;

//(2)继承
export const ArrayMethods = Object.create(oldArrayProtoMethods);
//  劫持
let method = ["push", "pop", "shift", "unshift", "splice", "sort", "reverse"];
method.forEach((i) => {
  ArrayMethods[i] = function (...args) {
    // console.log(args, "args123");
    let result = oldArrayProtoMethods[i].apply(this, args);
    let inserted;

    switch (i) {
      case "push":
        method = "push";
      case "unshift":
        method = "unshift";
        inserted = args;
        break;
      case "splice":
        method = "splice";
        inserted = args.splice(2);
    }
    let ob = this.__ob__;

    if (inserted) ob.observeArray(inserted);
    ob.dep.notify();
    return result;
  };
});
